#include <irrlicht.h>
#include <cstdlib> // rand
using namespace irr;
#pragma comment(lib, "Irrlicht.lib") 

#include "CAxialParticleSystemSceneNode.h"
  
scene::CAxialParticle *p;

int main()
{
    IrrlichtDevice *device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(800, 600),
                          32, false, false, false);

    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager(); 
   
    scene::ICameraSceneNode *cam = smgr->addCameraSceneNodeFPS(0,50,100,10);
    cam->setPosition(core::vector3df(0,-300,200));
    cam->setTarget(core::vector3df(0,0,0));
    
    scene::ISceneNode *test = smgr->addCubeSceneNode(1);
    test->setScale(core::vector3df(500,1,500));
    test->setPosition(core::vector3df(0,-350,0));
    video::ITexture * floor = driver->getTexture("./media/floor.png");
	test->setMaterialFlag(video::EMF_LIGHTING, false);
    test->setMaterialTexture( 0, floor );

	core::stringc ppo = device->getFileSystem()->getWorkingDirectory();

    
    scene::CAxialParticleSystemSceneNode *ps = 
           new scene::CAxialParticleSystemSceneNode(smgr->getRootSceneNode(), smgr, 0, core::dimension2d<s32>(16,1));
    
    ps->setMaterialFlag(video::EMF_LIGHTING, false);
    ps->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL); // makes it transparent
    video::ITexture * txture = driver->getTexture("./media/rain.png");

    ps->setMaterialTexture( 0, txture );
    

    ps->setAxis(0); // rain uses the x axis

    while(device->run())
    {
	// an extremely crappy way to do an emitter, but its just a demo
        if (ps->getParticleCount() < 4000)
        {
            ps->addParticles(10);  
    
            p = &ps->getParticles()[ps->getParticleCount()-10];
            
            u32 time = device->getTimer()->getTime();
            for(int n=0; n<10; n++)
            {
                p[n].pos = core::vector3df((rand() % 6000 - 3000)/10.0f, 
                                           (rand() % 3000)/10.0f, 
                                           (rand() % 6000 - 3000)/10.0f);
                
                
                p[n].velocity = core::vector3df(0.0f,-(rand() % 50) / 1000.0f-0.5,0.00f);
				p[n].spin= core::vector3df(0.0f, 0.0f, 0.0f);
				p[n].rot= core::vector3df(0.0f, 0.0f, 0.0f);
                
                p[n].size = rand() % 10 + 5;
                p[n].endTime = time + 1000;
            }
        }

        driver->beginScene(true, true, video::SColor(255,64,64,64));
        smgr->drawAll();
        driver->endScene(); 
    }

    device->drop();

    return 0;
}
